#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<vector>
#include<functional>
#include"task.h"

typedef std::function<void()> task_t;

void LoadTask(std::vector<task_t> &tasks)
{
    tasks.push_back(PrintLog);
    tasks.push_back(Download);
    tasks.push_back(Backup);
}

int main()
{
    std::vector<task_t> tasks;
    LoadTask(tasks);

    pid_t id = fork();
    if(id == 0)
    {
        while(true)
        {
            printf("我是子进程，我的PID：%d\n", getpid());
            sleep(1);
        }
        exit(0);
    }
    else
    {
        while(true)
        {
            pid_t rid = waitpid(id, nullptr, WNOHANG);
            if(rid > 0)
            {
                printf("等待子进程%d成功\n", rid);
                break;
            }
            else if(rid < 0)
            {
                printf("等待子进程%d失败\n", rid);
            }
            else
            {
                printf("子进程尚未退出\n");
                // 父进程做自己的事情
                for(auto &task : tasks)
                {
                    task();
                }
            }
            sleep(1);
        }
    }
    return 0;
}




































/*
enum{
    OK = 0,
    OPEN_FILE_ERROR,
};

const std::string gsep = " ";
std::vector<int> data;

int SaveBegin()
{
    std::string name = std::to_string(time(nullptr));
    name += ".backup";
    FILE* fp = fopen(name.c_str(), "w");
    if(fp == nullptr) return OPEN_FILE_ERROR;
    std::string dataStr;
    for(auto d : data)
    {
        dataStr += std::to_string(d);
        dataStr += gsep;
    }
    fputs(dataStr.c_str(), fp);
    fclose(fp);
    return OK;
}

void Save()
{
    pid_t id = fork();
    if(id == 0)
    {
        int code = SaveBegin();
        exit(code);
    }
    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if(rid > 0)
    {
        int code = WEXITSTATUS(status);
        if(code == 0) printf("备份成功, exit code:%d\n", code);
        else printf("备份失败. exit code:%d\n", code);
    }
    else
    {
        perror("waitpid");
    }
}

int main()
{
    int cnt = 1;
    while(true)
    {
        data.push_back(cnt ++);
        sleep(1);

        if(cnt % 10 == 0)
        {
            Save();
        }
    }
    return 0;
}*/


/*
int main()
{
    pid_t id = fork();
    if(id < 0)
    {
        printf("errno:%d, errstring:%s\n", errno, strerror(errno));
        return errno;
    }
    else if(id == 0)
    {
        int cnt = 5;
       while(cnt --)
       {
            printf("我是子进程，PID：%d\n", getpid());
            sleep(1);
       }
       exit(123);
    }
    else
    {
        sleep(10);
        //pid_t rid = wait(nullptr);
        int status = 0;
        pid_t rid = waitpid(id, &status, 0);
        if(rid > 0)
        {
            if(WIFEXITED(status))  // 只有进程正常退出，才去查看它的退出码
            {
                printf("wait sub process success, rid：%d, status code:%d\n", rid, WEXITSTATUS(status));
            }
            else  // 说明进程异常退出
            {
                printf("child process quit errno\n");
            }
        }
        else
        {
            perror("waitpid");
        }
        while(true)
        {
            printf("我是父进程，PID：%d\n", getpid());
            sleep(1);
        }
    }
    return 0;
}*/
